package com.invoice.analysis.ofd.service;

import com.invoice.analysis.entity.InvoiceEntity;
import com.invoice.analysis.functional.ThreeVoid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.w3c.dom.Node;

/**
 * @author 郑恩远
 * @version 1.0
 * @date 2024/11/8 15:18:00
 */
public enum EnumInvoiceType {
  INVOICE_NO("ofd:InvoiceNo", "发票号码", 1) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name, InvoiceEntity::setInvoiceNumber);
      this.threeVoid = service::setCommon;
    }
  },
  INVOICE_NO_TRAIN("2399, 发票号码", "发票号码", 2) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name, InvoiceEntity::setInvoiceNumber);
      this.idThreeVoid = service::setTrainCommon;
    }
  },
  BUYER_NAME("ofd:BuyerName", "购方名称", 1) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name, InvoiceEntity::setBuyerName);
      this.threeVoid = service::setCommon;
    }
  },
  BUYER_NAME_TRAIN("2416", "购买方名称:", 2) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name, InvoiceEntity::setBuyerName);
      this.idThreeVoid = service::setTrainCommon;
    }
  },
  BUYER_TAX_ID("ofd:BuyerTaxID", "购方税号", 1) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name, InvoiceEntity::setBuyerTaxID);
      this.threeVoid = service::setCommon;
    }
  },
  BUYER_TAX_ID_TRAIN("2417, 购买方纳税人识别号", "购方税号", 2) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name, InvoiceEntity::setBuyerTaxID);
      this.idThreeVoid = service::setTrainCommon;
    }
  },
  SELLER_NAME("ofd:SellerName", "销售方名称", 1) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name, InvoiceEntity::setSellerName);
      this.threeVoid = service::setCommon;
    }
  },
  SELLER_TAX_ID("ofd:SellerTaxID", "销售方税号", 1) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name, InvoiceEntity::setSellerTaxID);
      this.threeVoid = service::setCommon;
    }
  },
  SELLER_TAX_ID_TRAIN("销售方纳税人识别号", "销售方税号", 2) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name, InvoiceEntity::setSellerTaxID);
      this.idThreeVoid = service::setTrainCommon;
    }
  },
  TAX_EXCLUSIVE_TOTAL_AMOUNT("ofd:TaxExclusiveTotalAmount", "合计金额", 1) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name);
      this.service.consumerBigDecimal = InvoiceEntity::setTaxExclusiveTotalAmount;
      this.threeVoid = service::setAmount;
    }
  },
  TAX_EXCLUSIVE_TOTAL_AMOUNT_TRAIN("合计金额", "合计金额", 2) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name);
      this.service.consumerBigDecimal = InvoiceEntity::setTaxExclusiveTotalAmount;
      this.idThreeVoid = service::setTrainAmount;
    }
  },
  TAX_TOTAL_AMOUNT("ofd:TaxTotalAmount", "税额", 1) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name);
      this.service.consumerBigDecimal = InvoiceEntity::setTaxTotalAmount;
      this.threeVoid = service::setAmount;
    }
  },
  TAX_TOTAL_TRAIN("合计税额", "合计税额", 2) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name);
      this.service.consumerBigDecimal = InvoiceEntity::setTaxTotalAmount;
      this.idThreeVoid = service::setTrainAmount;
    }
  },
  TAX_INCLUSIVE_TOTAL_AMOUNT("ofd:TaxInclusiveTotalAmount", "含税总额", 1) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name);
      this.service.consumerBigDecimal = InvoiceEntity::setTaxInclusiveTotalAmount;
      this.threeVoid = service::setAmount;
    }
  },
  TAX_INCLUSIVE_TOTAL_AMOUNT_TRAIN("2411, 价税合计", "含税总额", 2) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name);
      this.service.consumerBigDecimal = InvoiceEntity::setTaxInclusiveTotalAmount;
      this.idThreeVoid = service::setTrainAmount;
    }
  },
  ISSUE_DATE("ofd:IssueDate", "开票日期", 1) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name, InvoiceEntity::setIssueDate);
      this.threeVoid = service::setCommon;
    }
  },
  ISSUE_DATE_TRAIN("2405, 开票日期", "开票日期", 2) {
    public void init() {
      super.init();
      this.service = new InvoiceService(this.codes, this.name, InvoiceEntity::setIssueDate);
      this.idThreeVoid = service::setTrainCommon;
    }
  };
  public final String code;
  public final Set<String> codes;
  public final String name;
  InvoiceService service;
  public ThreeVoid<Node, InvoiceEntity, Map<String, String>> threeVoid;
  public ThreeVoid<String, InvoiceEntity, Map<String, String>> idThreeVoid;
  public final int mode;
  private static Map<String, EnumInvoiceType> collect;
  EnumInvoiceType(String code, String name, int mode) {
    this.code = code;
    this.codes = new HashSet<>(Arrays.asList(code.split(", *")));
    this.name = name;
    this.mode = mode;
    this.init();
  }
  public void init() {}
  public static Map<String, EnumInvoiceType> getValues() {
    if (collect == null) {
      EnumInvoiceType[] values = EnumInvoiceType.values();
      collect =  new HashMap<>();
      for (EnumInvoiceType value : values) {
        value.codes.forEach(code -> collect.put(code, value));
      }
    }
    return collect;
  }
}
